const API = require('../../../pages/api/index')
const {
  typeJson
} = require('../../../utils/typejson')
let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
const {
  get,
  post,
  put
} = require('../../../request/index')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pulldown: "",
    pullup: 2,
    addButton: true,
    Refresh: false,
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    active: 0,
    alarm1: [],
    alarm2: [],
    alarm3: [],
    activeItem: [],
    show: false,
    show1: false,
    pageNum: 1,
    pageSize: 10,
    showCom: true,
    flowDetails: {},
    equipmentList: [{
      part: "",
      model: "",
      unit: "",
      num: "",
      manufacturer: "",
    }],
    parmes: {
      auditUserId: 1,
      content: "",
      flowNo: '',
      id: '',
      sign: "",
      status: 3
    },
  },
  async change1() {
    let {
      userId,
    } = this.data.prolist
    const res = await post('/app/emergsummie/getList', {
      userId: userId,
      type: 8
    })
    if (res.code == 200) {
      this.setData({
        alarm1: res.rows,
        alarm2: [],
        alarm3: []
      })
    }

  },
  //待审核
  async change2() {
    let {
      userId,
    } = this.data.prolist
    let {
      pageNum,
      pageSize,
    } = this.data
    const res = await get(`/app/emergsummie/listByMyslef/1`, {
      pageNum,
      pageSize,
    })
    if (res.code == 200) {
      this.setData({
        alarm1: [],
        alarm2: res.rows,
        alarm3: []

      })
    }
  },
  async change3() {
    let {
      userId,
    } = this.data.prolist
    const res = await post('/app/emergsummie/getList', {
      userId: userId,
      type: 8,
      approStatus: 2
    })
    if (res.code == 200) {
      this.setData({
        alarm1: [],
        alarm2: [],
        alarm3: res.rows
      })
    }
  },
  onChange1(event) {
    let value = "parmes.auditUserId"
    this.setData({
      [value]: event.detail,
    });
  },
  onChange(event) {
    console.log(event.detail);
    this.setData({
      pageNum: 1,
      active: event.detail.index
    })
    switch (event.detail.index) {
      case 0:
        this.change1()
        break;
      case 1:
        this.change2()
        break;
      case 2:
        this.change3()
        break;
      default:
        break;
    }
  },
  // 检修相关 start--
  examine(e) {
    let item = e.target.dataset.flowdetails
    let value = 'parmes.id'
    let flowNo = 'parmes.flowNo'
    this.setData({
      [value]: item.id,
      [flowNo]: item.flowNo,
      show1: true
    })
  },
  onChangeInput(e) {
    this.setData({
      [e.target.dataset.lable]: e.detail
    })
  },
  canvasChang(e) {
    let value = 'parmes.sign'; // 注意这里哈
    this.setData({
      [value]: e.detail
    })
  },
  submit() {
    let obj = Object.assign({}, this.data.parmes)
    put('/app/emergsummie', obj).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          duration: 3000,
          icon: 'success',
          mask: true
        })
        this.onClose()
      }
    })
    console.log(obj);
  },
  // 检修相关 end--
  look(e) {
    let item = e.target.dataset.flowdetails
    this.setData({
      activeItem: item,
      flowDetails: item.flowDetails[0],
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false,
      show1: false,
      activeItem: [],
      parmes: {
        auditUserId: 1,
        content: "",
        flowNo: '',
        id: '',
        sign: "",
        status: 3
      },
    })
    this.onChange({
      detail: {
        index: this.data.active
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.change1()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    this.setData({
      showCom: false
    })

    this.setData({
      showCom: true
    })
  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      Refresh: true
    })
    wx.hideLoading();
    wx.stopPullDownRefresh()
    this.setData({
      Refresh: false
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pullup: this.data.pullup + 1
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})